import {request} from '@/utils/request'

// ==================== 类型定义 ====================

export interface Exhibition {
  id: number
  title: string
  subtitle?: string
  description?: string
  location: string
  start_date: string
  end_date: string
  opening_hours?: string
  image_url?: string[] // 修改为字符串数组，支持多张图片
  status: 'upcoming' | 'ongoing' | 'ended'
  highlights?: string[]
  notices?: string[]
  created_at?: string
  updated_at?: string
}

export interface TicketType {
  id: number
  exhibition_id: number
  name: string
  description?: string
  price: number
  stock: number
  sold?: number // 已售数量
  available?: number // 剩余数量（-1 表示无限）
  is_sold_out?: boolean // 是否售罄
  created_at?: string
  updated_at?: string
}

export interface OrderItem {
  ticket_type_id: number
  quantity: number
}

export interface TicketInfo {
  visitor_name: string
  visitor_phone: string
  visitor_id_card: string
}

export interface CreateOrderData {
  exhibition_id: number
  buyer_name: string
  buyer_phone: string
  buyer_id_card: string
  items: OrderItem[]
  tickets: TicketInfo[]
}

export interface Order {
  id: number
  order_no: string
  exhibition_id: number
  exhibition_title?: string
  exhibition_location?: string
  start_date?: string
  end_date?: string
  buyer_name: string
  buyer_phone: string
  buyer_id_card: string
  total_amount: number
  status: 'pending' | 'paid' | 'cancelled'
  verification_code: string
  created_at: string
  updated_at?: string
  items?: OrderItemDetail[]
  tickets?: Ticket[]
}

export interface OrderItemDetail {
  id: number
  order_id: number
  ticket_type_id: number
  ticket_type_name: string
  quantity: number
  price: number
  subtotal: number
}

export interface Ticket {
  id: number
  order_id: number
  order_item_id: number
  ticket_type_name: string
  visitor_name: string
  visitor_phone: string
  visitor_id_card: string
  verification_code: string
  is_used: number
  used_at?: string
  created_at: string
}

export interface User {
  id: number
  phone: string
  email?: string
  created_at?: string
}

export interface LoginData {
  phone: string
  password: string
}

export interface RegisterData {
  phone: string
  password: string
  email?: string
}

export interface AuthResponse {
  token: string
  user: User
}

// ==================== 认证 API ====================

// 用户注册
export async function register(data: RegisterData) {
  return request<AuthResponse>({
    url: '/auth/register',
    method: 'POST',
    data
  })
}

// 用户登录
export async function login(data: LoginData) {
  return request<AuthResponse>({
    url: '/auth/login',
    method: 'POST',
    data
  })
}

// 获取当前用户信息
export async function getCurrentUser() {
  return request<User>({
    url: '/auth/me',
    method: 'GET',
    needAuth: true
  })
}

// ==================== 展览 API ====================

// 获取展览列表
export async function getExhibitions(status?: string) {
  const url = status ? `/exhibitions?status=${status}` : '/exhibitions'
  return request<Exhibition[]>({
    url,
    method: 'GET',
    needAuth: true
  })
}

// 获取展览详情
export async function getExhibition(id: number) {
  return request<Exhibition>({
    url: `/exhibitions/${id}`,
    method: 'GET',
    needAuth: true
  })
}

// 获取票种列表
export async function getTicketTypes(exhibitionId: number) {
  return request<TicketType[]>({
    url: `/exhibitions/${exhibitionId}/ticket-types`,
    method: 'GET',
    needAuth: true
  })
}

// ==================== 订单 API ====================

// 创建订单
export async function createOrder(data: CreateOrderData) {
  return request<{
    order_no: string
    order_id: number
    total_amount: number
    verification_code: string
  }>({
    url: '/orders',
    method: 'POST',
    data,
    needAuth: true
  })
}

// 获取订单详情
export async function getOrder(orderNo: string) {
  return request<Order>({
    url: `/orders/${orderNo}`,
    method: 'GET',
    needAuth: true
  })
}

// 根据手机号查询订单
export async function getOrdersByPhone(phone: string) {
  return request<Order[]>({
    url: `/orders/phone/${phone}`,
    method: 'GET',
    needAuth: true
  })
}

// 获取当前用户的订单
export async function getMyOrders() {
  return request<Order[]>({
    url: '/orders/my',
    method: 'GET',
    needAuth: true
  })
}

// 更新订单状态
export async function updateOrderStatus(orderNo: string, status: string) {
  return request<{order_no: string; status: string}>({
    url: `/orders/${orderNo}/status`,
    method: 'PUT',
    needAuth: true,
    data: {status}
  })
}

// ==================== 常用联系人相关 ====================

export interface Contact {
  id: string
  user_id: string
  name: string
  phone: string
  id_card: string
  is_default: boolean
  created_at: string
  updated_at: string
}

export interface CreateContactData {
  name: string
  phone: string
  id_card: string
  is_default?: boolean
}

export interface UpdateContactData {
  name?: string
  phone?: string
  id_card?: string
  is_default?: boolean
}

// 获取当前用户的所有联系人
export async function getContacts() {
  return request<Contact[]>({
    url: '/contacts',
    method: 'GET',
    needAuth: true
  })
}

// 获取联系人详情
export async function getContact(id: string) {
  return request<Contact>({
    url: `/contacts/${id}`,
    method: 'GET',
    needAuth: true
  })
}

// 创建联系人
export async function createContact(data: CreateContactData) {
  return request<Contact>({
    url: '/contacts',
    method: 'POST',
    needAuth: true,
    data
  })
}

// 更新联系人
export async function updateContact(id: string, data: UpdateContactData) {
  return request<Contact>({
    url: `/contacts/${id}`,
    method: 'PUT',
    needAuth: true,
    data
  })
}

// 删除联系人
export async function deleteContact(id: string) {
  return request<{message: string}>({
    url: `/contacts/${id}`,
    method: 'DELETE',
    needAuth: true
  })
}

// 设置默认联系人
export async function setDefaultContact(id: string) {
  return request<Contact>({
    url: `/contacts/${id}/default`,
    method: 'PUT',
    needAuth: true
  })
}
